// pages/goodsList/goodsList.js
import { require} from '../../require/index.js'
import  regeneratorRuntime, { async } from '../../lib/runtime/runtime.js'
// 下拉刷新，页面初初始化0，
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: '综合',
        isActive: false
      },
      {
        id: 0,
        value: '销售',
        isActive: true
      },
      {
        id: 0,
        isActive: false,
        value: '价格'
      }
    ],
    goodsList:[]

  },
  totalPage:0,
  QueryParams: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize:10,
    total: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options)
      this.QueryParams.cid = options.cid
      this.getList()
  },
  async getList() {
    const res = await require({url: '/goods/search', data: this.QueryParams})
    console.log(res)
    this.totalPage = Math.ceil(res.total/this.QueryParams.pagesize)
    this.setData({
      goodsList: [...this.data.goodsList, ...res.goods]
    }),
    wx.stopPullDownRefresh()
  }
  ,
  handleItemIndex (e) {
    console.log('ss',e)
    const tabs = [...this.data.tabs]
    tabs.forEach((v,i) => {
      v.isActive = i === e.detail.index ? true: false
    })
    this.setData({
      tabs
    })
  },
  onReachBottom() {
    if(this.QueryParams.pagenum >= this.totalPage){
      console.log('假装')
      wx.showToast({
        title: '没有下一页数据了',
      });
    } else {
      console.log('假装2')
      this.QueryParams.pagenum++
      this.getList()
    }
  },
  onPullDownRefresh(){
    console.log('11')
    this.setData({
      goodsList: []
    }),
    this.QueryParams.pagenum = 1
    this.getList()
  }
})